package BloodTimeHybernateTest;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;

import BloodTimeHybernateDO.ConfiguracionDO;

/**
 * 
 * @author Manuel A. Quintero V.
 * 
 */
public class TestConfiguracionDO {

	public static void main(String[] args) {
		Configuration configuration = new AnnotationConfiguration();
		configuration.configure("/BloodTimeHybernateDO/hibernate.cfg.xml");
		SessionFactory sessionFactory = configuration.buildSessionFactory();
		
		// ---------------------------------------------------------------
        // Insert
        // ---------------------------------------------------------------
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		ConfiguracionDO configuracion;
		for(int i=1;i<=10;i++){
		configuracion = new ConfiguracionDO();
		configuracion.setClave("clave"+i);
		configuracion.setTipo("tipo"+i);
		configuracion.setValor("valor"+i);
		
		session.save(configuracion);
		}
		session.getTransaction().commit();
		session.close();
		
		// ---------------------------------------------------------------
        // Load
        // ---------------------------------------------------------------

		session = sessionFactory.openSession();
		session.beginTransaction();

		configuracion = null;
		configuracion = (ConfiguracionDO) session.load(ConfiguracionDO.class, 1);
		System.out.println("Configuracion.clave: " + configuracion.getClave());
		
		// ---------------------------------------------------------------
        // Update
        // ---------------------------------------------------------------
		configuracion.setClave("otra clave");

		session.getTransaction().commit();
		session.close();

		// ---------------------------------------------------------------
        // Query
        // ---------------------------------------------------------------

		session = sessionFactory.openSession();
		session.beginTransaction();

		configuracion = null;
		configuracion = (ConfiguracionDO) session.createQuery("FROM ConfiguracionDO AS c WHERE c.id = '2'").uniqueResult();
		System.out.println("configuracion.clave: " + configuracion.getClave());

			
		// ---------------------------------------------------------------
        // Delete
        // ---------------------------------------------------------------
		session.delete(configuracion);

		session.getTransaction().commit();
		session.close();

		// ---------------------------------------------------------------
        // List
        // ---------------------------------------------------------------

		session = sessionFactory.openSession();
		session.beginTransaction();

		List<ConfiguracionDO> list = session.createCriteria(ConfiguracionDO.class).list();
		for (ConfiguracionDO conf : list) {
			System.out.println(conf.getClave());
		}

		session.getTransaction().commit();
		session.close();

	}

}
